package com.matrix.java.chap_05;

import java.util.Scanner;

/*
    财务应用程序：显示分期还贷时间表
 */
public class Ex5_22 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Loan Amount: ");
        double amount = input.nextDouble();
        System.out.print("Number of Years: ");
        int year = input.nextInt();
        System.out.print("Annual Interest Rate: ");
        double annualInterestRate = input.nextDouble();
        double monthPayment;
        double totalPayment;
        String blank = "        ";
        double monthRate = annualInterestRate / (12 * 100);
        monthPayment = (amount * monthRate) / (1 - (1 / (Math.pow((1 + monthRate), year * 12))));
        System.out.printf("%s%.2f%n", "Monthly Payment: ", monthPayment);
        totalPayment = monthPayment * year * 12;
        System.out.printf("%s%.2f%n", "Total Payment: ", totalPayment);
        System.out.println("Payment#" + blank + "Interest" + "Principal" + "Balance");
        double interest;
        double principal = 0;
        double balance = amount;
        for (int payment = 1; payment <= year * 12; payment++) {
            interest = monthRate * balance;
            if (payment == year * 12) {
                principal = balance;
            } else {
                principal = monthPayment - interest;
            }
            balance -= principal;
            System.out.printf("%d%s%.2f%s%.2f%s%.2f%n", payment, blank, interest, blank, principal, blank, balance);
        }
        input.close();
    }
}
